//
//  MyAlreadyGetKaQuanHeaderView.m
//  FD
//
//  Created by Yasin on 2018/10/18.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  已经领取卡券的表头view

#import "MyAlreadyGetKaQuanHeaderView.h"
@interface MyAlreadyGetKaQuanHeaderView()
/**标题文字*/
@property (nonatomic,  strong) UIButton *btn;
/**标记选中的button*/
@property (nonatomic,  strong) UIButton *selectedButton;

/**红色线*/
@property (nonatomic,  strong) UIView *redLine;

/**button 数组 后面需要根据网络请求的数据更新按钮的文字，所以先保存一下按钮*/
@property (nonatomic,  strong) NSMutableArray *btnArrM;


@end

@implementation MyAlreadyGetKaQuanHeaderView

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray<NSString *> *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViewWithTitles:titles];
//        self.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}

- (void)setupViewWithTitles:(NSArray <NSString *> *)titles
{
    CGFloat w = self.bounds.size.width;
    CGFloat h = self.bounds.size.height;
    
    //按钮文字的宽度
    CGFloat btn_w = w / titles.count;
    
    for (int i=0; i<titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        button.tag = i; //设置标记
        
        [button setTitle:titles[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//        button.backgroundColor = [UIColor yellowColor];
        [button addTarget:self action:@selector(clickTitleButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //设置frame
        CGFloat btn_x = btn_w*i;
        button.frame = CGRectMake(btn_x, 0, btn_w, h);
        
        //存储一下按钮数组
        [self.btnArrM addObject:button];
        
        
        [self addSubview:button];
    }
    
    //标记第一个按钮
    UIButton *firstButton = self.subviews.firstObject;
    
    //添加红色指示线
    self.redLine = [[UIView alloc] init];
    self.redLine.backgroundColor = [UIColor redColor];
    self.redLine.rb_height = 2;
    self.redLine.rb_width = 80;
    self.redLine.rb_y = self.rb_height - 3;
    self.redLine.rb_center_x = firstButton.rb_center_x;
    [self addSubview:self.redLine];
    
    //一开始不要调用：导致下看不见
//    [self titleButtonClick:firstButton];
}

- (void)titleButtonClick:(UIButton *)button
{
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    
    //指示器移动
    [UIView animateWithDuration:0.25 animations:^{
        self.redLine.rb_width = button.titleLabel.rb_width;
        self.redLine.rb_center_x = button.rb_center_x;
    }];
}


//getter
- (NSMutableArray *)btnArrM {
    if (!_btnArrM) {
        _btnArrM = [NSMutableArray array];
    }
    return _btnArrM;
}

- (void)clickTitleButtonAction:(UIButton *)button
{
    NSLog(@"点击了:%ld---%@",button.tag, button.titleLabel.text);
    [self titleButtonClick:button];
    
    //事件回调
    
    if (self.clickTitleButtonBlock) {
        self.clickTitleButtonBlock(button);
    }
}


#pragma mark  - 接口
- (void)updateTitles:(NSArray *)titleArr {
    
    if(titleArr.count == 0) return;
    
    for (int i = 0; i<self.btnArrM.count; i++) {
        UIButton *btn = self.btnArrM[i];
        //标题
        NSString *btnTitle = titleArr[i];
        
        [btn setTitle:btnTitle forState:(UIControlStateNormal)];
    }
}
@end
